package si.cir.util;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import si.cir.entities.Napoved;
import si.cir.entities.Vreme;
import si.cir.entities.Zagovor;

public class VremeHandler extends DefaultHandler{
	
	private List<Vreme> trenutnaNapoved = new ArrayList<Vreme>();
	private List<Napoved> napovedi = new ArrayList<Napoved>();
	private Vreme TNapoved;
	private Napoved napoved;
	private String currentElement = "";
	private int element = 0;

	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		
		if(qName.equals("trenutne_razmere"))	{
			TNapoved = new Vreme();
			element = 0;
		}
		else if(qName.equals("napoved"))
		{
			//napoved = new Napoved();
			element = 1;
		}
		else if(qName.equals("dan"))
		{
			napoved = new Napoved();
			napoved.setDan(attributes.getValue("dan_v_tednu"));
		}
	}	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equals("trenutne_razmere")){
			trenutnaNapoved.add(TNapoved);
			currentElement = "";
		}
		
		else if (qName.equals("razmere")&& element == 0)
		{
			TNapoved.setRazmere(currentElement);
		}
		
		else if (qName.equals("temperatura"))
		{
			TNapoved.setTemperatura(currentElement);
		}
		else if (qName.equals("vlaznost"))
		{
			TNapoved.setVlaznost(currentElement);
		}
		else if (qName.equals("veter_smer"))
		{
			TNapoved.setVeterSmer(currentElement);
		}
		else if (qName.equals("veter_hitrost"))
		{
			TNapoved.setVeterHitrost(currentElement);
		}
		else if (qName.equals("dan"))
		{
			napovedi.add(napoved);
		}
		else if(qName.equals("razmere") && element == 1)
		{
			napoved.setRazmere(currentElement);
		}
		
		else if(qName.equals("najnizja_temperatura") && element == 1)
		{
			napoved.setNajnizjaTemperatura(currentElement);
		}
		else if(qName.equals("najvisja_temperatura") && element == 1)
		{
			napoved.setNajvisjaTemperatura(currentElement);
			
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		currentElement = new String(ch, start, length);
	}
	
	public List<Vreme> getVreme(){
		return trenutnaNapoved;
	}
	
	public List<Napoved> getNapoved()
	{
		return napovedi;
	}
}

